package com.quasar.hpatient.view;

import android.graphics.Bitmap;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.custommodel.PhotoModel;
import com.quasar.hpatient.model.event.DeletePhotoEvent;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.onInterface.OnPhotoListener;
import com.quasar.hpatient.presenter.ImagePresenter;
import com.quasar.hpatient.utils.StringUtils;
import com.quasar.hpatient.view.adapter.ViewPagerAdapter;
import com.quasar.hpatient.widget.CustomFAB;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 删除图片
 */
@EActivity(R.layout.activity_delete_photo)
public class DeletePhotoActivity extends BaseActivity implements OnPhotoListener {


    private ImagePresenter imagePresenter;
    private ViewPagerAdapter mviewPagerAdapter;
    @ViewById(R.id.cf_imageButton)
    CustomFAB cf_imageButton;
    private List<ImageView> mList;
    @ViewById(R.id.pager)
    ViewPager mPager;
    @Extra
    int position;
    @ViewById(R.id.textsize)
    TextView textsize;
    String uris;
    @Extra
    ArrayList<PhotoModel> photoModelDatas;

    @AfterViews
    void init(){
        initToolbar();
        initView();
        initData();
    }

    @Override
    protected void initView() {
        imagePresenter = new ImagePresenter(this);
        mList = new ArrayList<ImageView>();
        PhotoView view;
        for (int i = 0; i < photoModelDatas.size(); i++) {
            view = new PhotoView(DeletePhotoActivity.this);
            view.enable();
            Glide.with(DeletePhotoActivity.this)
                    .load(photoModelDatas.get(i).getOriginalPath())
                    .asBitmap()
                    .into(view);

            mList.add(view);
        }
    }

    int photo_position;
    @Override
    protected void initData() {
        mviewPagerAdapter = new ViewPagerAdapter(mList);
        mPager.setAdapter(mviewPagerAdapter);

        if (position != 0) {
            mPager.setCurrentItem(position);
        }
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int position, float positionOffset, int positionOffsetPixels) {


                photo_position = position + 1;
                if (photoModelDatas.size() != 0) {
                    textsize.setText(position + 1 + "/" + photoModelDatas.size());
                }

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    //删除按钮
    @Click(R.id.cf_imageButton)
    public void cf_imageButton() {
        if (photo_position != 0) {
            for (int i = 0; i < photoModelDatas.size(); i++) {
                uris = photoModelDatas.get(photo_position - 1).getOriginalPath();
            }

            //添加异常
            try {
                final PhotoModel photoModel=photoModelDatas.get(photo_position - 1);

                if (photoModel != null) {
                    ViseLog.d("保存图片+picListBean");

                    SimpleDialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
                        @Override
                        public void onPositiveActionClicked(DialogFragment fragment) {
                            super.onPositiveActionClicked(fragment);
                            DeletePhotoEvent deletePhotoEvent=new DeletePhotoEvent();
                            deletePhotoEvent.photoId=photo_position-1;
                            EventBus.getDefault().post(deletePhotoEvent);
                            finish();
                        }

                        @Override
                        public void onNegativeActionClicked(DialogFragment fragment) {
                            super.onNegativeActionClicked(fragment);
                        }
                    };
                    builder.title("确定删除化验图片？").positiveAction("删除").negativeAction("取消");
                    ((SimpleDialog.Builder) builder).message("注：图片可提高医生对化验单做出的诊断(删除网络图片后本地图片需要重新选取)");
                    DialogFragment fragment = DialogFragment.newInstance(builder);
                    fragment.show(getSupportFragmentManager(), null);
                }
            } catch (Exception e) {
                //删除本地图片
                SimpleDialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
                    @Override
                    public void onPositiveActionClicked(DialogFragment fragment) {
                        super.onPositiveActionClicked(fragment);
                    }

                    @Override
                    public void onNegativeActionClicked(DialogFragment fragment) {
                        super.onNegativeActionClicked(fragment);
                    }
                };
                builder.title("确定删除化验图片？").positiveAction("删除").negativeAction("取消");
                ((SimpleDialog.Builder) builder).message("注：图片可提高医生对化验单做出的诊断");
                DialogFragment fragment = DialogFragment.newInstance(builder);
                fragment.show(getSupportFragmentManager(), null);
            }
        }
    }

    @Override
    public void onViewSuccess(Bitmap data) {

    }

    @Override
    public void onViewError(String msg) {

    }

    @Override
    public void onViewRefreshUI() {

    }

    @Override
    public void onDeletePtoto(DataStateResult dataStateResult) {

    }
}
